/**
 * weapp-js v2.2.3(2022-4-26)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2022 Motigo, Inc.
 * 
 */
import BaseRequest from './base_request.js';



class Monitor {
    constructor () {
        this.appid = '';
        this._statBuf = [];
        this._statTimer = null;
    }
    init (appid) {
        this.appid = appid;
    }
    stat (key,elapsed_time) {
        this._statBuf.push({
            key: key,
            elapsed_time: elapsed_time
        });

        if(this._statTimer) {
            clearTimeout(this._statTimer);
        }

        this._statTimer = setTimeout(()=>{
            try {
                let pvuvData = [];
                this._statBuf.forEach(item=>{
                    pvuvData.push({
                        key: item.key,
                        elapsed_time: item.elapsed_time || 0
                    })
                })

                if (pvuvData.length) {
                    BaseRequest.post(`/data/pvuv/weapp_${this.appid}`, pvuvData).then().catch(()=>{});
                }
                this._statBuf = [];
            } catch(e) {
                console.error('exception when report monitor data, err:', e);
            }
        }, 5000);
    }
}


export default new Monitor();